package org.darchitect.nn.som.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import org.darchitect.ui.UIUtil;

public class CreateNewSomDialog extends JDialog {
	
	private static final long serialVersionUID = -8235589069911672423L;
	
	private JSpinner kohonenLayerWidthSpr;
	private JSpinner inputVectorSizeSpr;
	private JSpinner d0tFactorSpr;
	private JSpinner a0tSpr;
	private JSpinner iterationsSpr;
	
	public CreateNewSomDialog(int kohonenLayerWidth, int inputVectorSize, double d0tFactor,
			double a0t, int iterations, ActionListener createListener) {
		JPanel comps = new JPanel();
		comps.setLayout(new GridBagLayout());
		JLabel kohonenLayerWidthLbl = new JLabel("Kohonen Layer Width");
		JLabel inputVectorSizeLbl = new JLabel("Input Vector Size");
		JLabel d0tFactorLbl = new JLabel("d0t Factor");
		JLabel a0tLbl = new JLabel("a0t");
		JLabel iterationsLbl = new JLabel("Iteration number");
		kohonenLayerWidthSpr = new JSpinner(new SpinnerNumberModel(kohonenLayerWidth, 1, 256, 1));
		inputVectorSizeSpr = new JSpinner(new SpinnerNumberModel(inputVectorSize, 1, 128, 1));
		d0tFactorSpr = new JSpinner(new SpinnerNumberModel(d0tFactor, 0.001, 1, 0.01));
		a0tSpr = new JSpinner(new SpinnerNumberModel(a0t, 0.001, 1, 0.01));
		iterationsSpr = new JSpinner(new SpinnerNumberModel(iterations, 1, 100000, 1));
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(3, 4, 2, 4);
		gbc.gridx = gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.fill = GridBagConstraints.NONE;
		comps.add(kohonenLayerWidthLbl, gbc);
		gbc.gridx = 1;
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		comps.add(kohonenLayerWidthSpr, gbc);
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.fill = GridBagConstraints.NONE;
		comps.add(inputVectorSizeLbl, gbc);
		gbc.gridx = 1;
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		comps.add(inputVectorSizeSpr, gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.fill = GridBagConstraints.NONE;
		comps.add(d0tFactorLbl, gbc);
		gbc.gridx = 1;
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		comps.add(d0tFactorSpr, gbc);
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.fill = GridBagConstraints.NONE;
		comps.add(a0tLbl, gbc);
		gbc.gridx = 1;
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		comps.add(a0tSpr, gbc);
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.anchor = GridBagConstraints.LINE_END;
		gbc.fill = GridBagConstraints.NONE;
		comps.add(iterationsLbl, gbc);
		gbc.gridx = 1;
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		comps.add(iterationsSpr, gbc);
		
		Box mainBox = Box.createVerticalBox();
		
		JButton createBtn = new JButton("Create");
		createBtn.addActionListener(createListener);
			
		JButton cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		Box btnBox = Box.createHorizontalBox();
		btnBox.add(createBtn);
		btnBox.add(Box.createHorizontalStrut(10));
		btnBox.add(cancelBtn);
		
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(comps);
		mainBox.add(Box.createVerticalStrut(7));
		mainBox.add(btnBox);
		mainBox.add(Box.createVerticalStrut(10));
		
		add(mainBox);
		pack();
		UIUtil.centerWindow(this);
		setVisible(true);
		setTitle("Create SOM");
	}

	public int getKohonenLayerWidth() {
		SpinnerNumberModel n = (SpinnerNumberModel) kohonenLayerWidthSpr.getModel();
		return n.getNumber().intValue();
	}

	public int getInputVectorSize() {
		SpinnerNumberModel n = (SpinnerNumberModel) inputVectorSizeSpr.getModel();
		return n.getNumber().intValue();
	}

	public double getD0tFactor() {
		SpinnerNumberModel n = (SpinnerNumberModel) d0tFactorSpr.getModel();
		return n.getNumber().doubleValue();
	}

	public double getA0t() {
		SpinnerNumberModel n = (SpinnerNumberModel) a0tSpr.getModel();
		return n.getNumber().doubleValue();
	}

	public int getIterations() {
		SpinnerNumberModel n = (SpinnerNumberModel) iterationsSpr.getModel();
		return n.getNumber().intValue();
	}
	
}
